﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;
using Microsoft.ApplicationBlocks.Data;

namespace AirlineCommon
{
    public class Flight
    {
        //private const string CONNECTION_STRING = "AirlineConnectionString";
        private Sector _sector;

        public Sector Sector
        {
            get { return _sector; }
            set { _sector = value; }
        }
        private List<PassengerCommon> _passengerList;

        public List<PassengerCommon> PassengerList
        {
            get { return _passengerList; }
            set { _passengerList = value; }
        }

        private string _FlightNo;
        private string _DepTime;
        private string _ArrTime;
        private DateTime _FlightDate;
        private int _FirstClassSeatsAvailable;
        private int _BusinessClassSeatsAvailable;
        private int _EconomyClassSeatsAvailable;
        public string DepTime
        {
            get { return _DepTime; }
            set { _DepTime = value; }
        }
        

        public string ArrTime
        {
            get { return _ArrTime; }
            set { _ArrTime = value; }
        }
        public string FlightNo
        {
            get { return _FlightNo; }
            set { _FlightNo = value; }
        }
        

        public DateTime FlightDate
        {
            get { return _FlightDate; }
            set { _FlightDate = value; }
        }
       

        public int FirstClassSeatsAvailable
        {
            get { return _FirstClassSeatsAvailable; }
            set { _FirstClassSeatsAvailable = value; }
        }
        

        public int BusinessClassSeatsAvailable
        {
            get { return _BusinessClassSeatsAvailable; }
            set { _BusinessClassSeatsAvailable = value; }
        }
        

        public int EconomyClassSeatsAvailable
        {
            get { return _EconomyClassSeatsAvailable; }
            set { _EconomyClassSeatsAvailable = value; }
        }
        public List<Flight> GetFlightList()
        {
            string strCONN = DAL.DatabaseProvider.AirlineConnectionString;
            string sql = "SELECT dbo.Flights.FlightNo, dbo.Flights.DepTime, dbo.Flights.ArrTime, ";
            sql+="dbo.ScheduledFlights.FlightDate, dbo.ScheduledFlights.FirstClassSeatsAvailable, ";
            sql+="dbo.ScheduledFlights.BusinessClassSeatsAvailable, dbo.ScheduledFlights.EconomyClassSeatsAvailable";
            sql+=" FROM dbo.Sector INNER JOIN";
            sql+=" dbo.Flights ON dbo.Sector.SectorID = dbo.Flights.SectorID INNER JOIN";
            sql+=" dbo.ScheduledFlights ON dbo.Flights.FlightNo = dbo.ScheduledFlights.FlightNo";
            sql +=" Where Sector.SectorID=@SectorID";
            try
            {
                List<Flight> list = new List<Flight>();
                SqlParameter pSectorID = new SqlParameter("@SectorID", _sector.SectorID);
                DataSet ds = SqlHelper.ExecuteDataset(strCONN, CommandType.Text, sql, pSectorID);
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    Flight fl = new Flight();
                    fl.FlightNo = dr[0].ToString();
                    fl.DepTime = dr[1].ToString();
                    fl.ArrTime = dr[2].ToString();
                    fl.FlightDate = DateTime.Parse(dr[3].ToString());
                    fl.FirstClassSeatsAvailable = int.Parse(dr[4].ToString());
                    fl.BusinessClassSeatsAvailable = int.Parse(dr[5].ToString());
                    fl.EconomyClassSeatsAvailable = int.Parse(dr[6].ToString());
                    list.Add(fl);
                }
                return list ;
            }
            catch (Exception ex)
            {
                //Log Exception (will be provided in next release)
                AppLogger.Error("Error Executing in Get Flight List", ex, "Flight", "GetFlightList");
                throw;
            }
        }
        public bool ReserTicket(PassengerCommon passenger, Ticket ticket,TravelClass travel)
        {
            SqlConnection conn = new SqlConnection(DAL.DatabaseProvider.AirlineConnectionString);
            if (conn.State == ConnectionState.Closed)
                conn.Open();
            SqlTransaction trans = conn.BeginTransaction();
            try
            {
                if (passenger.InsertPassenger(trans) == 1 && ticket.InsertDaillyCollection(trans) == 1  &&
                    travel.UpdateClassSeatForReservation(trans, _FlightNo, _FlightDate) == 1)
                {
                    trans.Commit();
                    return true;
                }
                else
                {
                    trans.Rollback();
                    return false;
                }
            }

            catch

            {
                trans.Rollback();
                return false;
            }
            if (conn.State == ConnectionState.Open)
                conn.Close();
        }
        public DataSet GetScheduleFlightList()
        {
            string strCONN = DAL.DatabaseProvider.AirlineConnectionString;
            string sql = "Select * from ScheduledFlights";
            return SqlHelper.ExecuteDataset(strCONN, CommandType.Text, sql);
        }
    }
}
